<template>
  <div class="contract-detail-container">
    <div class="header">
      <h2>合同详情1</h2>
      <div class="breadcrumb">
        <router-link to="/contracts">合同管理</router-link> >
        合同详情
      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-indicator">
      <div class="spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 错误消息 -->
    <div v-if="errorMessage" class="error-message">
      <i class="fa fa-exclamation-circle mr-2"></i>{{ errorMessage }}
    </div>

    <!-- 合同基本信息（状态显示中文） -->
    <div v-if="contract && !loading" class="card">
      <div class="card-body">
        <h3 class="contract-name">{{ contract.name }}</h3>
        <div class="contract-info">
          <p><strong>合同ID:</strong> {{ contract.id }}</p>
          <p><strong>客户:</strong> {{ contract.customer?.companyName || '未选择' }}</p>
          <p><strong>项目经理:</strong> {{ projectManagerName }}</p>
          <!-- 核心修改：状态显示中文并添加样式 -->
          <p><strong>状态:</strong>
            <span :class="`status-tag ${getStatusClass(contract.status)}`">
              {{ getStatusChineseName(contract.status) }}
            </span>
          </p>
          <p><strong>创建时间:</strong> {{ formatDate(contract.createTime) }}</p>
          <p><strong>更新时间:</strong> {{ formatDate(contract.updateTime) }}</p>
        </div>

        <div class="clauses-section mt-4">
          <h4>条款列表</h4>
          <div v-if="clauses.length > 0" class="clauses-list">
            <ul class="list-group">
              <li
                  v-for="clause in clauses"
                  :key="clause.id"
                  class="list-group-item"
              >
                {{ clause.content }}
              </li>
            </ul>
          </div>
          <div v-else class="empty-message text-center py-2">
            <i class="fa fa-info-circle text-muted mr-2"></i>暂无条款
          </div>
        </div>
      </div>
    </div>

    <div class="button-group mt-4 d-flex justify-content-center gap-3">
      <button
          @click="goBack"
          class="btn btn-primary"
      >
        <i class="fa fa-arrow-left mr-2"></i>返回上一级
      </button>
      <button
          @click="goToContractEdit"
          class="btn btn-info"
      >
        <i class="fa fa-edit mr-2"></i>编辑合同
      </button>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'ContractDetail',
  data() {
    return {
      contractId: null,
      contract: null,
      clauses: [],
      errorMessage: '',
      loading: true,
      // 新增：状态映射表
      statusMap: {
        DRAFT: "草稿",
        PENDING_REVIEW: "待审核",
        REVIEW_FAILED: "审核失败",
        PENDING_CONFIRMATION: "待确认",
        CONFIRMED: "已确认",
        COMPLETED: "已完成",
        ARCHIVED: "已归档"
      },
      // 新增：状态样式映射
      statusClassMap: {
        DRAFT: "bg-gray-100 text-gray-700",
        PENDING_REVIEW: "bg-yellow-100 text-yellow-800",
        REVIEW_FAILED: "bg-red-100 text-red-800",
        PENDING_CONFIRMATION: "bg-blue-100 text-blue-800",
        CONFIRMED: "bg-green-100 text-green-800",
        COMPLETED: "bg-teal-100 text-teal-800",
        ARCHIVED: "bg-purple-100 text-purple-800"
      }
    }
  },
  computed: {
    projectManagerName() {
      return this.contract?.projectManager?.name || '未指定'
    }
  },
  created() {
    this.contractId = this.$route.params.contractId
    this.fetchContractDetails()
    this.fetchClauses()
  },
  methods: {
    // 新增：获取状态中文名称
    getStatusChineseName(statusKey) {
      return this.statusMap[statusKey] || statusKey
    },

    // 新增：获取状态样式类
    getStatusClass(statusKey) {
      return this.statusClassMap[statusKey] || "bg-gray-100 text-gray-700"
    },

    fetchContractDetails() {
      if (!this.contractId) {
        this.errorMessage = '缺少合同ID参数'
        setTimeout(() => {
          this.$router.push('/contracts')
        }, 2000)
        return
      }

      axios.get(`/auth/contracts/${this.contractId}`)
          .then(response => {
            this.contract = response.data
          })
          .catch(err => {
            console.error('获取合同详情失败', err)
            this.errorMessage = '获取合同详情失败，请重试'
          })
          .finally(() => {
            this.loading = false
          })
    },

    fetchClauses() {
      if (!this.contractId) return

      axios.get(`/auth/clauses/contract/${this.contractId}`)
          .then(response => {
            this.clauses = response.data
          })
          .catch(err => {
            console.error('获取合同条款失败', err)
            this.errorMessage = '获取合同条款失败，请重试'
          })
    },

    goBack() {
      this.$router.go(-1)
    },

    goToContractEdit() {
      this.$router.push(`/contracts/${this.contractId}/edit`)
    },

    formatDate(dateString) {
      if (!dateString) return ''
      const date = new Date(dateString)
      return date.toLocaleString()
    }
  }
}
</script>

<style scoped>
.contract-detail-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  margin-bottom: 30px;
}

.breadcrumb {
  font-size: 0.9rem;
  color: #6c757d;
  margin-top: 10px;
}

.breadcrumb router-link {
  color: #007bff;
  text-decoration: none;
}

.breadcrumb router-link:hover {
  text-decoration: underline;
}

.loading-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  min-height: 300px;
}

.spinner {
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-left-color: #007bff;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.error-message {
  color: #721c24;
  background-color: #f8d7da;
  border-left: 4px solid #f5c6cb;
  padding: 12px 15px;
  border-radius: 0 4px 4px 0;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.card {
  margin-bottom: 20px;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.card:hover {
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.card-body {
  padding: 20px;
}

.contract-name {
  margin-bottom: 20px;
  font-size: 1.5rem;
  color: #333;
}

.contract-info {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 20px;
}

.contract-info p {
  margin: 5px 0;
  flex: 1 1 300px;
}

.clauses-section h4 {
  margin-top: 0;
  padding-bottom: 10px;
  border-bottom: 1px solid #e9ecef;
}

.clauses-list {
  margin-bottom: 0;
}

.empty-message {
  color: #6c757d;
  font-style: italic;
  padding: 10px 0;
}

.btn {
  padding: 10px 20px;
  border-radius: 4px;
  font-weight: 500;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-primary {
  background-color: #007bff;
  color: white;
}

.btn-primary:hover {
  background-color: #0069d9;
  transform: translateY(-2px);
  box-shadow: 0 4px 6px rgba(0, 123, 255, 0.2);
}

.btn-info {
  background-color: #17a2b8;
  color: white;
}

.btn-info:hover {
  background-color: #138496;
  transform: translateY(-2px);
  box-shadow: 0 4px 6px rgba(23, 162, 184, 0.2);
}

.button-group {
  margin-top: 30px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

/* 新增：状态标签样式 */
.status-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.9em;
  font-weight: 500;
  text-transform: capitalize;
}

.bg-gray-100 { background-color: #f3f4f6; }
.text-gray-700 { color: #374151; }

.bg-yellow-100 { background-color: #fff9e6; }
.text-yellow-800 { color: #854d0e; }

.bg-red-100 { background-color: #fee2e2; }
.text-red-800 { color: #991b1b; }

.bg-blue-100 { background-color: #dbeafe; }
.text-blue-800 { color: #1e40af; }

.bg-green-100 { background-color: #dcfce7; }
.text-green-800 { color: #166534; }

.bg-teal-100 { background-color: #ccfbf1; }
.text-teal-800 { color: #134e4a; }

.bg-purple-100 { background-color: #f3e8ff; }
.text-purple-800 { color: #5b21b6; }
</style>